#include<p18cxxx.h>
#include"config.h"

#include<delays.h>

#define RS_TRIS TRISAbits.TRISA2
#define RW_TRIS TRISAbits.TRISA3
#define EN_TRIS TRISAbits.TRISA4
#define LCDDATA_TRIS TRISD
#define LCDFLAG_TRIS TRISDbits.TRISD7
#define LCDFLAG PORTDbits.RD7
#define LCDDATA LATD
#define RS LATAbits.LATA2
#define RW LATAbits.LATA3
#define EN LATAbits.LATA4
#define v_ratio 346

void display_voltage(unsigned int volt);




void Busy_LCD(void)		//check whether LCD is busy
{
RS_TRIS=0;
RW_TRIS=0;
EN_TRIS=0;
LCDDATA_TRIS=0;
LCDFLAG_TRIS=1;
RS=0;
RW=1;
EN=1;
while(LCDFLAG);
EN=0;
LCDFLAG_TRIS=0;
}
void lcd_data(unsigned int ch)		//sends character to LCD display
{ 
Busy_LCD();
RS=1;
RW=0;
EN=1;
LCDDATA=ch;
EN=0;
} 
void Inst_LCD(unsigned int ch)		//sends instructions to LCD
{
Busy_LCD();
RS=0;
RW=0;
EN=1;
LCDDATA=ch;
EN=0;
}

void lcd_stat(void)
{
Inst_LCD(56);
Inst_LCD(12);
Inst_LCD(1);
}




void display_voltage(unsigned int volt)
{
unsigned char num[6]={0,0,0,0,0,0};

while(volt>99999)
{
volt=volt-100000;
num[5]++;
}
while(volt>9999)
{
volt=volt-10000;
num[4]++;
}
while(volt>999)
{
volt=volt-1000;
num[3]++;
}
while(volt>99)
{
volt=volt-100;
num[2]++;
}
while(volt>9)
{
volt=volt-10;
num[1]++;
}
num[0]=volt;

lcd_stat();
lcd_data(num[5]+48);
lcd_data(num[4]+48);
lcd_data(num[3]+48);
lcd_data('.');
lcd_data(num[2]+48);
lcd_data(num[1]+48);
lcd_data('V');
}



void character(void) 
{
int *ptr;
int a[8]={0x10,0x08,0x0c,0x04,0x06,0x02,0x01,0};
int b[8]={0x01,0x02,0x06,0x04,0x0c,0x08,0x10,0};

Inst_LCD(0x40);
ptr=&a[0];
while(*ptr)
{
lcd_data(*ptr);
ptr++;
}
Delay10KTCYx(80);
Inst_LCD(0x40);
ptr=&b[0];
while(*ptr)
{
lcd_data(*ptr);
ptr++;
}
Inst_LCD(0xc0);
lcd_data(0);
}
void mode(void)
{
unsigned char *p;
unsigned char msg1[10]="manual";
p=&msg1[0];
Inst_LCD(0x88);
while(*p)
{
lcd_data(*p);
p++;
}
}
